package controller.v2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import model.BoardVO;
import model.MemberVO;
import service.v2.BoardService;
import service.v2.ConfigService;
import service.v2.MemberService;
import service.v2.MessageService;
import util.PackageUtil;
import util.PropertieUtil;


@RequestMapping("/v2/notice")
@Controller
public class BoardController {
	@Autowired
	private BoardService boardService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private MessageService messageService;
	
	/**
	 * 公告列表
	 */
	@ResponseBody
	@RequestMapping(value="/noticeList",method=RequestMethod.GET)
	public Object noticeList(@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response, BoardVO boardVO) throws Exception{
		
		String host = configService.loadConfig("HOST");

		boardVO.setCat_id(PropertieUtil.loadPropertie("app_notice"));
		int total = boardService.getTotal(boardVO);
		boardVO.setPagination(boardVO.getPage_index(), total+"");
		//boardVO.setUrl(configService.loadConfig("WECHAT_HOST")+"/notice_Detail?notice_id=");
	    boardVO.setUrl(host+"/v2/page/noticeDetail?board_id=");
		//boardVO.setUrl("http://10.0.0.68:8080/v2/page/noticeDetail?board_id=");
		List<BoardVO> boardList = boardService.getBoardList(boardVO);
		if(token != null && member_id != null){
			MemberVO memberVO = memberService.getMemberByIdCheck(member_id ,token, os);
			if(memberVO != null){
				//清0 友米头条数量
				messageService.resetMemberMsg(member_id, "1");
				/*List<BoardVO> boardListAll = boardService.getBoardListUnReadAll(boardVO,member_id);
				if(boardListAll.size() != 0){
					boardService.insertRead(boardListAll, member_id);
				}*/
			}
		}
		 Map<String, Object> map = new HashMap<String, Object>();
	    map.put("pagination", boardVO);
	    map.put("list", boardList);
	    return PackageUtil.packageParams(200,"success",map,"");
	}
	/**
	 * 公告详细
	 */
	@ResponseBody
	@RequestMapping(value="/noticeDetail/{notice_id}",method=RequestMethod.GET)
	public Object noticeDetail(@PathVariable String notice_id,HttpServletRequest request, HttpServletResponse response, BoardVO boardVO) throws Exception{
		boardVO = boardService.getBoardById(notice_id);
		if(boardVO==null){
			return PackageUtil.packageParams(10011,"数据不存在","",""); 
		}
		return PackageUtil.packageParams(200,"success",boardVO,""); 
	}
	
}
